import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_list_view/flutter_list_view.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/util/utils.dart';
import 'package:oktoast/oktoast.dart';
import 'package:sticky_headers/sticky_headers.dart';

import '../../a_yyt/models/address_book_entity.dart';
class AddressBookContentFragment extends StatefulWidget{
  List<AddressBookInfo> list;

  AddressBookContentFragment({required this.list});

  @override
  State<StatefulWidget> createState() => _AddressBookContentState();

}

class _AddressBookContentState extends State<AddressBookContentFragment>{

  List<AddressBookData> listItem = [];

  @override
  void initState() {
    super.initState();
    var map = <String,List<AddressBookInfo>>{};
    List<AddressBookData> infoList = [];
    widget.list.sort((o1,o2){
      return o1.shortPy!.compareTo(o2.shortPy!);
    });
    for(var item in widget.list){
      if(map[item.firstPy!]==null){
        map[item.firstPy!] = [item];
      }else{
        map[item.firstPy!]!.add(item);
      }
    }
    map.forEach((key, value) {
      infoList.add(AddressBookData(key, value));
    });
    setState(() {
      listItem = infoList;
    });

  }

  @override
  Widget build(BuildContext context) {
    return FlutterListView.separated(itemBuilder: (context,index){
      var item = listItem[index];
      return StickyHeader(header: CommonText(item.typeName).addToContainer(
        padding: EdgeInsets.symmetric(horizontal: 16,vertical: 10),
        width: double.infinity,
        color: Colours.bg_color
      ), content: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          SizedBox(height: 12,),
          ...item.list.map((e) => Row(
            children: [
              SizedBox(width: 16,),
              CommonText(e.nickName,textColor: Colors.white,textSize: 18,fontWeight: FontWeight.bold).addToContainer(
                  width: 60,height: 60,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      gradient: LinearGradient(colors: [
                        Colors.blue,Colors.blue[400]!,Colors.blue[200]!
                      ],begin: Alignment.bottomRight,end: Alignment.topLeft),
                      borderRadius: 8.borderRadius
                  )
              ),
              SizedBox(width: 20,),
              Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  CommonText(e.name),
                  SizedBox(height: 13,),
                  CommonText(e.mobile)
                ],
              ).setWeight(1),
              Column(
                children: [
                  Icon(Icons.phone_forwarded_rounded,color: Colors.white,size: 20,).addToContainer(
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: Colors.blue
                    ),
                    width: 30,height: 30
                  ).onTap(() {
                    showBottomSelect(context, items: ['拨打电话','发送短信','复制号码'],onSelect: (item){
                      if(item=='拨打电话'){
                        Utils.launchTelURL(e.mobile??"");
                      }
                      if(item=='发送短信'){
                        Utils.launchSmsURL(e.mobile??"");
                      }
                      if(item=='复制号码'){
                        if(!e.mobile.isNullOrEmpty()){
                          Utils.copyToClipboard(e.mobile??"");
                          showToast("已复制到剪切板");
                        }
                      }
                    });

                  }),
                ],
              ),
              SizedBox(width: 16,),
            ],
          )).toList().insertWidget(SizedBox(height: 15,)),
          SizedBox(height: 12,),
        ],
      ));
    }, separatorBuilder: (context,index){
      return Container();
    }, itemCount: listItem.length);
  }

}

class AddressBookData{
  String typeName;
  List<AddressBookInfo> list;

  AddressBookData(this.typeName, this.list);

}

